Replace a builtin command using cabbrev
You can use :command to define your own commands, but user-defined commands must start with an uppercase letter to avoid confusion with built-in commands. This tip shows how to replace a built-in command using :cabbrev to automatically replace the existing command when it is typed. For example, you may want to change the behavior of a built-in command like :e. Or, you may make a common typo, like using :w1 instead of :w!. For whatever reason, if you want to change the behavior of a built-in command, you can't just use :command to override it, because user-defined commands must begin with a capital letter. Suppose you have a user-defined :E command that you want to use to override the default :e command. You could do the following: :cabbrev e =(getcmdtype() ':' && getcmdpos() 1 ? 'E' : 'e') The (getcmdtype() ':' && getcmdpos()) makes sure the replacement happens only in the first column of the command line (i.e. not later in the line, where it is most likely NOT intended to be used as a command, and not on the search line, which is also affected by cabbrev). If you do this a lot, it would be useful to define a function to do it for you. Use this to quickly and easily define lowercase abbreviations for whatever command you want: function! CommandCabbr(abbreviation, expansion) execute 'cabbr ' . a:abbreviation . ' =getcmdpos() 1 && getcmdtype() ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"' endfunction command! -nargs=+ CommandCabbr call CommandCabbr() " Use it on itself to define a simpler abbreviation for itself. CommandCabbr ccab CommandCabbr This not only creates the function, but also provides the (lowercase!) command :ccab to define such abbreviations "on the fly". Note that because of the use of , you will need to escape ("\ "), or use , to include literal spaces in your expansion. This is explained in . WARNING: the abbreviation commands work only if "set paste" is NOT set before. (see ":help paste" for more information. Example to avoid w1 typo: cabbrev w1 =(getcmdtype() ':' && getcmdpos() 1 ? 'w!' : 'w1') Scripts * Create aliases for Vim commands. * Alter built-in Ex commands by your own ones. * You can use ambiguous command. * Make aliases to vim command. See also These are all good candidates for this technique (or use it already): *Find in files within Vim *Handle common command typos *Run a command in multiple buffers This is a good reference in case this tip causes you problems: *Disabling cabbrev This plugin does the same thing (possibly in the same way): * References * * * * Comments Using (Vim 7) With Vim 7 ("or later"), you can use :cabbrev — here is an example, replacing the :h command to open the help window at the bottom of the Vim screen: cabbrev h ((getcmdtype() ':' && getcmdpos() <= 2)? 'bot h' : 'h') See also getcmdpos() will usually be 2 because the cursor is on the space after :h (I tested it with getcmdpos() 1 and that doesn't work). -- Tonymec 07:03, 12 May 2008 (UTC) :In this example, you can do ::h or : h (from normal mode) to call the built-in :help command (bypassing the abbreviation). :: or :he or :help since abbreviations only trigger when followed by a non-id character (such as a space or the Enter key) — Tonymec 18:01, 21 August 2008 (UTC) Example of overriding message on writing - will print 'Saved' after :w in lieu of filename 42 lines, 42 characters written. command Write :silent w | :echo 'Saved' cabbrev w =(getcmdtype() ':' && getcmdpos() 1 ? 'Write' : 'w')